package com.shuoyue.ycgk.utils;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class HorizontaltemColorableDecoration extends RecyclerView.ItemDecoration {
    private int space;
    private boolean useside = true;

    public HorizontaltemColorableDecoration(int space) {
        this.space = space;
    }

    public HorizontaltemColorableDecoration(int space, boolean useside) {
        this.space = space;
        this.useside = useside;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {
        if (useside) {
            outRect.top = space;
            outRect.bottom = space;
            if (parent.getChildLayoutPosition(view) == 0) {
                outRect.left = space;
            }
            outRect.right = space;
        } else {
            if (parent.getChildLayoutPosition(view) != 0) {
                outRect.left = space;
            }
        }
    }

    @Override
    public void onDraw(@NonNull Canvas c,
                       @NonNull RecyclerView parent,
                       @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);

//        final int left = parent.getPaddingLeft();
//        final int right = parent.getWidth() - parent.getPaddingRight();
//        final int childCount = parent.getChildCount();
//        for (int i = 0; i < childCount; i++) {
//            final View child = parent.getChildAt(i);
//            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
//            final int top = child.getBottom() + params.bottomMargin + Math.round(ViewCompat.getTranslationZ(child));
//            final int bottom = top + drawable.getIntrinsicHeight();
//            drawable.setBounds(left, top + 2, right, bottom);//宽度调节
//            drawable.setColorFilter(context.getResources().getColor(R.color.colorAccent), PorterDuff.Mode.XOR);//Mode选择不对无法正常显示选择的颜色
//            drawable.draw(c);
//        }
    }
}